var http = require('http');
var settings = require('../config');
var iconv = require('iconv-lite');
var crypto = require('crypto');

exports.get = function(path, callback) {
	var timestamp = new Date().getTime();
	var md5 = crypto.createHash('md5');
	var signStr = 'timestamp=' + timestamp + '&safekey=' + settings.safekey;
	var sign = md5.update(signStr).digest('hex').toLowerCase();
	var options = {
		host: settings.javaServerHost,
		port: settings.javaServerHostport,
		path: path,
		method: 'GET',
		headers: {
			'Content-Type': 'application/json;charset=utf-8',
			'timestamp': timestamp,
			'crpdb_auth': sign
		}
	}
	var req1 = http.request(options, function(res1) {
		if (res1.statusCode !== 200) {
			var Time = new Date();
			var ReTime = Time.getFullYear() + '-' + (Time.getMonth() + 1) + '-' + Time.getDate() + ' ' + Time.getHours() + ':' + Time.getMinutes();
			console.warn(ReTime + ' GET请求数据库错误');
			console.warn(ReTime + '请求URL：' + path);
			var errMessage = JSON.stringify({
				'status': false,
				'message': ReTime + ' GET请求数据' + res1.statusCode + '错误，请联系系统管理员'
			});
			return callback(errMessage);
		}
		var postDataStr = [];
		var size = 0;
		res1.on('data', function(chunk) {
			postDataStr.push(chunk);
			size += chunk.length;
		})
		res1.on('end', function() {
			var buf = Buffer.concat(postDataStr, size);
			var str = iconv.decode(buf, 'utf8');
			var posts = JSON.parse(str);
			return callback(posts);
		})
	}).on('error', function(err) {
		if (err) {
			options = JSON.stringify(options);
			var Time = new Date();
			var ReTime = Time.getFullYear() + '-' + (Time.getMonth() + 1) + '-' + Time.getDate() + ' ' + Time.getHours() + ':' + Time.getMinutes();
			console.warn(ReTime + ' GET请求数据库错误,请求头：' + options + ',错误代码：' + err);
			return callback('数据库连接超时，请联系系统管理员');
		}
	})
	req1.end();
	return this;
}

exports.post = function(postData, path, callback) {
	var timestamp = new Date().getTime();
	var md5 = crypto.createHash('md5');
	var signStr = 'timestamp=' + timestamp + '&safekey=' + settings.safekey;
	var sign = md5.update(signStr).digest('hex').toLowerCase();
	var options = {
		host: settings.javaServerHost,
		port: settings.javaServerHostport,
		path: path,
		method: 'POST',
		headers: {
			'Content-Type': 'application/json;charset=utf-8',
			'timestamp': timestamp,
			'crpdb_auth': sign
		}
	}
	var req1 = http.request(options, function(res1) {
		if (res1.statusCode !== 200) {
			options = JSON.stringify(options);
			var Time = new Date();
			var ReTime = Time.getFullYear() + '-' + (Time.getMonth() + 1) + '-' + Time.getDate() + ' ' + Time.getHours() + ':' + Time.getMinutes();
			console.warn(ReTime + ' POST请求数据库错误');
			console.warn(ReTime + '请求头：' + options);
			var errMessage = JSON.stringify({
				'status': false,
				'message': ReTime + ' POST请求数据' + res1.statusCode + '错误，请联系系统管理员'
			})
			return callback(errMessage);
		}
		var postDataStr = [];
		var size = 0;
		res1.on('data', function(chunk) {
			postDataStr.push(chunk);
			size += chunk.length;
		})
		res1.on('end', function() {
			var buf = Buffer.concat(postDataStr, size);
			var str = iconv.decode(buf, 'utf8');
			var posts = JSON.parse(str);
			return callback(posts);
		})
	}).on('error', function(err) {
		if (err) {
			options = JSON.stringify(options);
			var Time = new Date();
			var ReTime = Time.getFullYear() + '-' + (Time.getMonth() + 1) + '-' + Time.getDate() + ' ' + Time.getHours() + ':' + Time.getMinutes();
			console.warn(ReTime + ' POST请求数据库错误,请求头：' + options + ',请求内容:' + postData + '，错误代码：' + err);
			return callback('数据库连接超时，请联系系统管理员');
		}
	})
	req1.write(postData + "\n");
	req1.end();
	return this;
}

exports.put = function(postData, path, callback) {
	var timestamp = new Date().getTime();
	var md5 = crypto.createHash('md5');
	var signStr = 'timestamp=' + timestamp + '&safekey=' + settings.safekey;
	var sign = md5.update(signStr).digest('hex').toLowerCase();
	var options = {
		host: settings.javaServerHost,
		port: settings.javaServerHostport,
		path: path,
		method: 'PUT',
		headers: {
			'Content-Type': 'application/json;charset=utf-8',
			'timestamp': timestamp,
			'crpdb_auth': sign
		}
	}
	var req1 = http.request(options, function(res1) {
		if (res1.statusCode !== 200) {
			var Time = new Date();
			var ReTime = Time.getFullYear() + '-' + (Time.getMonth() + 1) + '-' + Time.getDate() + ' ' + Time.getHours() + ':' + Time.getMinutes();
			console.warn(ReTime + ' PUT请求数据库错误');
			console.warn(ReTime + '请求URL：' + path);
			var errMessage = JSON.stringify({
				'status': false,
				'message': ReTime + ' POST请求数据' + res1.statusCode + '错误，请联系系统管理员'
			});
			return callback(errMessage);
		}
		var postDataStr = [];
		var size = 0;
		res1.on('data', function(chunk) {
			postDataStr.push(chunk);
			size += chunk.length;
		})
		res1.on('end', function() {
			var buf = Buffer.concat(postDataStr, size);
			var str = iconv.decode(buf, 'utf8');
			var posts = JSON.parse(str);
			return callback(posts);
		})
	}).on('error', function(err) {
		if (err) {
			options = JSON.stringify(options);
			var Time = new Date();
			var ReTime = Time.getFullYear() + '-' + (Time.getMonth() + 1) + '-' + Time.getDate() + ' ' + Time.getHours() + ':' + Time.getMinutes();
			warn.log(ReTime + ' PUT请求数据库错误,请求头：' + options + ',请求内容:' + postData + '，错误代码：' + err);
			return callback('数据库连接超时，请联系系统管理员');
		}
	})
	req1.write(postData + "\n");
	req1.end();
	return this;
}

exports.delete = function(postData, path, callback) {
	var timestamp = new Date().getTime();
	var md5 = crypto.createHash('md5');
	var signStr = 'timestamp=' + timestamp + '&safekey=' + settings.safekey;
	var sign = md5.update(signStr).digest('hex').toLowerCase();
	var options = {
		host: settings.javaServerHost,
		port: settings.javaServerHostport,
		path: path,
		method: 'DELETE',
		headers: {
			'Content-Type': 'application/json;charset=utf-8',
			'timestamp': timestamp,
			'crpdb_auth': sign
		}
	}
	var postDataStr = '';
	var req1 = http.request(options, function(res1) {
		if (res1.statusCode !== 200) {
			var Time = new Date();
			var ReTime = Time.getFullYear() + '-' + (Time.getMonth() + 1) + '-' + Time.getDate() + ' ' + Time.getHours() + ':' + Time.getMinutes();
			console.warn(ReTime + ' DELETE请求数据库错误');
			console.warn(ReTime + '请求URL：' + path);
			var errMessage = JSON.stringify({
				'status': false,
				'message': ReTime + ' POST请求数据错误，请联系系统管理员'
			})
			return callback(errMessage);
		}
		var postDataStr = [];
		var size = 0;
		res1.on('data', function(chunk) {
			postDataStr.push(chunk);
			size += chunk.length;
		})
		res1.on('end', function() {
			var buf = Buffer.concat(postDataStr, size);
			var str = iconv.decode(buf, 'utf8');
			var posts = JSON.parse(str);
			return callback(posts);
		})
	}).on('error', function(err) {
		if (err) {
			options = JSON.stringify(options);
			var Time = new Date();
			var ReTime = Time.getFullYear() + '-' + (Time.getMonth() + 1) + '-' + Time.getDate() + ' ' + Time.getHours() + ':' + Time.getMinutes();
			console.warn(ReTime + ' DELETE请求数据库错误,请求头：' + options + ',错误代码：' + err);
			return callback('数据库连接超时，请联系系统管理员');
		}
	})
	req1.end();
	return this;
}

exports.getNode = function(path, callback) {
	var options = {
		host: settings.nodeServerHost_inside,
		port: settings.nodeServerHostport,
		path: path,
		method: 'GET'
	}
	var req1 = http.request(options, function(res1) {
		if (res1.statusCode !== 200) {
			var Time = new Date();
			var ReTime = Time.getFullYear() + '-' + (Time.getMonth() + 1) + '-' + Time.getDate() + ' ' + Time.getHours() + ':' + Time.getMinutes();
			console.log(ReTime + ' GET请求数据库错误');
			console.log(ReTime + '请求URL：' + path);
			var errMessage = JSON.stringify({
				'status': false,
				'message': ReTime + ' GET请求数据错误，请联系系统管理员'
			})
			return callback(errMessage);
		}
		var postDataStr = [];
		var size = 0;
		res1.on('data', function(chunk) {
			postDataStr.push(chunk);
			size += chunk.length;
		})
		res1.on('end', function() {
			var buf = Buffer.concat(postDataStr, size);
			var str = iconv.decode(buf, 'utf8');
			var posts = JSON.parse(str);
			return callback(posts);
		})
	}).on('error', function(err) {
		if (err) {
			options = JSON.stringify(options);
			var Time = new Date();
			var ReTime = Time.getFullYear() + '-' + (Time.getMonth() + 1) + '-' + Time.getDate() + ' ' + Time.getHours() + ':' + Time.getMinutes();
			console.log(ReTime + ' GET请求数据库错误,请求头：' + options + ',错误代码：' + err);
			return callback('数据库连接超时，请联系系统管理员');
		}
	})
	req1.end();
	return this
}

exports.postNode = function(postData, path, callback) {
	var options = {
		host: settings.nodeServerHost_inside,
		port: settings.nodeServerHostport,
		path: path,
		method: 'POST',
		headers: {
			'Content-Type': 'application/json;charset=utf-8'
		}
	}
	var req1 = http.request(options, function(res1) {
		if (res1.statusCode !== 200) {
			options = JSON.stringify(options);
			var Time = new Date();
			var ReTime = Time.getFullYear() + '-' + (Time.getMonth() + 1) + '-' + Time.getDate() + ' ' + Time.getHours() + ':' + Time.getMinutes();
			console.log(ReTime + ' POST请求数据库错误');
			console.log(ReTime + '请求头：' + options);
			var errMessage = JSON.stringify({
				'status': false,
				'message': ReTime + ' POST请求数据错误，请联系系统管理员'
			})
			return callback(errMessage);
		}
		var postDataStr = [];
		var size = 0;
		res1.on('data', function(chunk) {
			postDataStr.push(chunk);
			size += chunk.length;
		})
		res1.on('end', function() {
			var buf = Buffer.concat(postDataStr, size);
			var str = iconv.decode(buf, 'utf8');
			var posts = JSON.parse(str);
			return callback(posts);
		});
	}).on('error', function(err) {
		if (err) {
			options = JSON.stringify(options);
			var Time = new Date();
			var ReTime = Time.getFullYear() + '-' + (Time.getMonth() + 1) + '-' + Time.getDate() + ' ' + Time.getHours() + ':' + Time.getMinutes();
			console.log(ReTime + ' POST请求数据库错误,请求头：' + options + ',请求内容:' + postData + '，错误代码：' + err);
			return callback('数据库连接超时，请联系系统管理员');
		}
	})
	req1.write(postData + "\n");
	req1.end();
	return this;
}